<template>
  <draggable :list="list" ghost-class="ghost" handle=".handle" item-key="id">
    <template #item="{ element }">
      <li class="list-item" :class="{ active: element.id === selectedId }" @click="handleClick(element.id)">
        <el-tooltip placement="top" :content="element.isHidden ? '显示' : '隐藏'">
          <el-button circle @click="handleChange(element.id, 'isHidden', !element.isHidden)">
            <template #icon>
              <el-icon v-if="element.isHidden"><View /></el-icon>
              <el-icon v-else><Hide /></el-icon>
            </template>
          </el-button>
        </el-tooltip>
        <el-tooltip placement="top" :content="element.isLocked ? '解锁' : '锁定'">
          <el-button circle @click="handleChange(element.id, 'isLocked', !element.isLocked)">
            <template #icon>
              <el-icon v-if="element.isLocked"><Unlock /></el-icon>
              <el-icon v-else><Lock /></el-icon>
            </template>
          </el-button>
        </el-tooltip>
        <inline-edit
          class="edit-area"
          :value="element.layerName"
          @change="value => handleChange(element.id, 'layerName', value)"
        ></inline-edit>
        <el-button class="handle" circle :icon="Rank"></el-button>
      </li>
    </template>
  </draggable>
</template>

<script lang="ts" setup>
import { View, Hide, Lock, Unlock, Rank } from '@element-plus/icons-vue'
import { ComponentData } from '../stores/editor'
import draggable from 'vuedraggable'

defineProps<{
  list: Array<ComponentData>
  selectedId?: string
}>()

const emits = defineEmits(['select', 'change', 'drop'])

function handleChange(id: string, key: string, value: boolean) {
  const data = {
    id,
    key,
    value,
    isRoot: true
  }
  emits('change', data)
}

function handleClick(id: string) {
  emits('select', id)
}
</script>

<style lang="scss" scoped>
.list-item {
  padding: 10px 15px;
  transition: all 0.5s ease-out;
  cursor: pointer;
  display: flex;
  align-items: center;
  //   justify-content: normal;
  border: 1px solid #fff;
  //   dcdfe6
  border-bottom-color: #f0f0f0;
}
.list-item.active {
  border: 1px solid #1890ff;
}

.list-item:hover {
  background: #e6f7ff;
}

.list-item .handle {
  cursor: move;
  margin-left: auto;
}
.list-item .edit-area {
  margin-left: 12px;
  // width: 100%;
}

.ghost {
  opacity: 0.5;
  background: #c8ebfb;
}
</style>
